package com.smart.reader.handler;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
 * @Author: JunYunXiao
 * @Date: 2025/1/9 14:52
 * @Description:
 */
@Component
public class CustomMetaObjectHandler implements MetaObjectHandler {
    public static final Long currentUserInfoId=1L;
    @Override
    public void insertFill(MetaObject metaObject) {
        // 创建时间
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        // 更新时间
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        // todo 创建⼈为空,则写⼊默认值(后期需要获取系统的登陆⼈ID)
        // String currentUserInfoId = SecurityContextHolder.getContext();
        if (ObjectUtil.isEmpty(getFieldValByName("createUser",metaObject))) {
            this.strictInsertFill(metaObject, "createUser", Long.class, currentUserInfoId);
        }
        if (ObjectUtil.isEmpty(getFieldValByName("updateUser",metaObject))) {
            this.strictInsertFill(metaObject, "updateUser", Long.class, currentUserInfoId);
        }
    }
    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时间
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
// String currentUserInfoId = SecurityContextHolder.getContext();
        if (ObjectUtil.isEmpty(getFieldValByName("updateUser",metaObject))) {
            this.strictInsertFill(metaObject, "updateUser", Long.class, currentUserInfoId);
        }
    }
}